package hc.bre.runner

import hc.bre.log.RuleLogger
import org.codehaus.groovy.control.CompilerConfiguration
import org.slf4j.Logger
import org.slf4j.LoggerFactory

abstract class RuleRunnerSupport {
	protected static final Logger _log = LoggerFactory.getLogger(RuleRunnerSupport.class);
	static {
		Object.metaClass.apply= {Closure rule ->
			try {
				RuleLogger.get().enter(delegate,rule);

				def ret = delegate.with(rule);

				RuleLogger.get().exit(ret);

				return ret;
			}
			catch(Throwable ex) {

				_log.warn("exception in apply:{}", ex)

				RuleLogger.get().exception(ex);

				throw ex;
			}
		};


		Object.metaClass.output= {msg->

			RuleLogger logger = RuleLogger.get();
			
			if(logger.rootRuleLog == null) {
				_log.error("output is called without rule context,message is:{}",msg);
				return;
			}

			if(msg != null) {
				logger.output(msg);
			}

			//TODO: return another powered output tools, eg: output().error(...)
			return null;
		};
	};
	
	String ruledir;

}
